#include "StatePattern.hpp"

Tokenizer::Tokenizer()
{
    /* create TokenStateList */
    TokenStateList.insert(std::make_pair(KEYWORD, new KeywordState()));
    TokenStateList.insert(std::make_pair(OPERATOR, new OperatorState()));
    TokenStateList.insert(std::make_pair(CONSTANT, new ConstantState()));
    TokenStateList.insert(std::make_pair(OTHER_PUNCTUATION, new OtherPunctuationState()));
}

Tokenizer::~Tokenizer()
{
    for (auto key : TokenStateList)
    {
        currentState = key.second;
        if (currentState != nullptr)
            delete currentState;
    }
}

Token Tokenizer::getNextToken(std::string::const_iterator char_ptr)
{
    if (std::isspace(*char_ptr))
        return Token();

    /* first word decide TokenStateType */
    if (std::isalpha(*char_ptr))
        currentState = TokenStateList[KEYWORD];
    else if (std::ispunct(*char_ptr))
        currentState = TokenStateList[OPERATOR];
    else if (std::isdigit(*char_ptr))
        currentState = TokenStateList[CONSTANT];
    else
        currentState = TokenStateList[OTHER_PUNCTUATION];

    return currentState->getToken(char_ptr);
}